<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-cookielib.html" />
<link rel="prev" href="module-SimpleHTTPServer.html" />
<link rel="parent" href="internet.html" />
<link rel="next" href="module-cookielib.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>18.21 CGIHTTPServer -- CGI-capable HTTP request handler</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="18.20 simplehttpserver  "
  href="module-SimpleHTTPServer.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="18. internet Protocols and"
  href="internet.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="18.22 cookielib  "
  href="module-cookielib.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-SimpleHTTPServer.html">18.20 SimpleHTTPServer  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="internet.html">18. Internet Protocols and</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-cookielib.html">18.22 cookielib  </a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h1><a name="SECTION00202100000000000000000">
18.21 <tt class="module">CGIHTTPServer</tt> --
         CGI-capable HTTP request handler</a>
</h1>

<p>
<a name="module-CGIHTTPServer"></a>

<p>
The <tt class="module">CGIHTTPServer</tt> module defines a request-handler class,
interface compatible with
<tt class="class">BaseHTTPServer.BaseHTTPRequestHandler</tt> and inherits behavior
from <tt class="class">SimpleHTTPServer.SimpleHTTPRequestHandler</tt> but can also
run CGI scripts.

<p>
<span class="note"><b class="label">Note:</b>
This module can run CGI scripts on <span class="Unix">Unix</span> and Windows systems;
on Mac OS it will only be able to run Python scripts within the same
process as itself.</span>

<p>
<span class="note"><b class="label">Note:</b>
CGI scripts run by the <tt class="class">CGIHTTPRequestHandler</tt> class cannot execute
redirects (HTTP code 302), because code 200 (script output follows)
is sent prior to execution of the CGI script.  This pre-empts the status
code.</span>

<p>
The <tt class="module">CGIHTTPServer</tt> module defines the following class:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-4355' xml:id='l2h-4355' class="class">CGIHTTPRequestHandler</tt></b>(</nobr></td>
  <td><var>request, client_address, server</var>)</td></tr></table></dt>
<dd>
This class is used to serve either files or output of CGI scripts from 
the current directory and below. Note that mapping HTTP hierarchic
structure to local directory structure is exactly as in
<tt class="class">SimpleHTTPServer.SimpleHTTPRequestHandler</tt>.

<p>
The class will however, run the CGI script, instead of serving it as a
file, if it guesses it to be a CGI script. Only directory-based CGI
are used -- the other common server configuration is to treat special
extensions as denoting CGI scripts.

<p>
The <tt class="function">do_GET()</tt> and <tt class="function">do_HEAD()</tt> functions are
modified to run CGI scripts and serve the output, instead of serving
files, if the request leads to somewhere below the
<code>cgi_directories</code> path.
</dl>

<p>
The <tt class="class">CGIHTTPRequestHandler</tt> defines the following data member:

<p>
<dl><dt><b><tt id='l2h-4356' xml:id='l2h-4356' class="member">cgi_directories</tt></b></dt>
<dd>
This defaults to <code>['/cgi-bin', '/htbin']</code> and describes
directories to treat as containing CGI scripts.
</dl>

<p>
The <tt class="class">CGIHTTPRequestHandler</tt> defines the following methods:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-4357' xml:id='l2h-4357' class="method">do_POST</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
This method serves the <code>'POST'</code> request type, only allowed for
CGI scripts.  Error 501, "Can only POST to CGI scripts", is output
when trying to POST to a non-CGI url.
</dl>

<p>
Note that CGI scripts will be run with UID of user nobody, for security
reasons. Problems with the CGI script will be translated to error 403.

<p>
For example usage, see the implementation of the <tt class="function">test()</tt>
function.

<p>
<div class="seealso">
  <p class="heading">See Also:</p>

  <dl compact="compact" class="seemodule">
    <dt>Module <b><tt class="module"><a href="module-BaseHTTPServer.html">BaseHTTPServer</a></tt>:</b>
    <dd>Base class implementation for Web server
                             and request handler.
  </dl>
</div>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="18.20 simplehttpserver  "
  href="module-SimpleHTTPServer.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="18. internet Protocols and"
  href="internet.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="18.22 cookielib  "
  href="module-cookielib.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-SimpleHTTPServer.html">18.20 SimpleHTTPServer  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="internet.html">18. Internet Protocols and</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-cookielib.html">18.22 cookielib  </a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
